package com.example.day9_sqlite2;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

//操作数据库中user表
public class UserDao {
    private MyHelper myHelper;
    private SQLiteDatabase sqLiteDatabase;
    public UserDao(Context context) {
        //创建数据库
        myHelper = new MyHelper(context);
        sqLiteDatabase = myHelper.getWritableDatabase();
    }
    //插入一条*******
    public void add(User user){
        ContentValues contentValues = new ContentValues();
        contentValues.put("name",user.getName());
        contentValues.put("password",user.getPassword());
        sqLiteDatabase.insert("user",null,contentValues);
    }
    //插入多条 一般要开启事务**********
    public void addAll(List<User> list){
        sqLiteDatabase.beginTransaction();
        for (User user : list) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("name",user.getName());
            contentValues.put("password",user.getPassword());
            sqLiteDatabase.insert("user",null,contentValues);
        }
        sqLiteDatabase.setTransactionSuccessful();
        sqLiteDatabase.endTransaction();
    }
    //查询全部******
    public List<User> selectAll(){
        ArrayList<User> list= new ArrayList<>();
        Cursor cursor = sqLiteDatabase.query("user", null, null, null, null, null, null);
        if(cursor != null){
            while (cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String password = cursor.getString(cursor.getColumnIndex("password"));
                list.add(new User(id,name,password));
            }
        }
        return list;

    }

    //根据一个******
    public User selectByName(User user){
        Cursor cursor = sqLiteDatabase.query("user", null, "name = ? and password = ?", new String[]{user.getName(), user.getPassword()},
                null, null, null);
        if(cursor != null){
            while (cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String password = cursor.getString(cursor.getColumnIndex("password"));
                return new User(id,name,password);
            }
        }
        return null;
    }

    public int deleteById(int id){
        int num = sqLiteDatabase.delete("user", "id = ?", new String[]{id+""});
        return num;
    }
    public int deleteByName(String name){
        int num = sqLiteDatabase.delete("user", "name = ?", new String[]{name + ""});
        return num;
    }

    public void update(User user){
        ContentValues contentValues = new ContentValues();
        contentValues.put("name",user.getName());
        sqLiteDatabase.update("user",contentValues,"id = ?",new String[]{user.getId()+""});
    }


}
